#include"Myrectitem.h"


MyRectItem::MyRectItem(qreal x, qreal y, qreal width, qreal height)
    : QGraphicsRectItem(x, y, width, height)
{
    setAcceptHoverEvents(true);  // 允许处理悬停事件
    setFlag(QGraphicsItem::ItemIsMovable);
}
MyRectItem::MyRectItem(QGraphicsRectItem &r)
    : QGraphicsRectItem(r.rect().x(),r.rect().y(),r.rect().width(),r.rect().height())
{

}



void MyRectItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    if(this->brush()==Qt::yellow)   return;
    setBrush(Qt::green);  // 鼠标悬停时改变颜色
    QGraphicsRectItem::hoverEnterEvent(event);  // 调用基类实现以确保正常处理
}
void MyRectItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    if(this->brush()==Qt::yellow)   return;
    setBrush(Qt::white);  //
    emit positionChanged();
    //emit selectedchanged(this->textItem->toPlainText());
    QGraphicsRectItem::hoverLeaveEvent(event);  // 调用基类实现以确保正常处理
}
void MyRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug() << "Mouse pressed";
    emit selectedchanged(this->textItem->toPlainText());

}

void MyRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    // 在鼠标移动时更新矩形的位置
    emit positionChanged();
    QGraphicsRectItem::mouseMoveEvent(event); // 调用基类的事件处理
}
